package util;

public class Log {
	public static int debug = 0xFF;
	
	public static final int ERROR_MASK 		= 0x01; // 0000 0001
	public static final int DEBUG_MASK 		= 0x02; // 0000 0010
	public static final int VERBOSE_MASK 	= 0x04; // 0000 0100
	public static final int ARRAY_MASK 		= 0x08; // 0000 1000
	
	public static final String HEADER_ERROR = "E)";
	public static final String HEADER_VERBOSE = "V)";
	public static final String HEADER_DEBUG = "D)";
	public static final String HEADER_SPECIAL = "The following output is special: ";
	public static void log(String tag, String str) {
		Log.d(tag, str);
	}
	public static void turnOn(int mask) {
		debug = debug | mask;
	}
	public static void turnOff(int mask) {
		debug = debug & ~mask;
	}
	public static void e(String tag, String str) {
		if ((debug & ERROR_MASK) != 0)
			System.out.println(HEADER_ERROR + " [" + tag + "] " + str);
	}
	public static void v(String tag, String str) {
		if ((debug & VERBOSE_MASK) != 0)
			System.out.println(HEADER_VERBOSE + " [" + tag + "] " + str);
	}
	public static void s(String tag) {
		if ((debug & VERBOSE_MASK) != 0)
			System.out.println("[" + tag + "] " + HEADER_SPECIAL);
	}
	public static void d(String tag, String str) {
		if ((debug & DEBUG_MASK) != 0)
			System.out.println(HEADER_DEBUG + " [" + tag + "] " + str);
	}
	public static void debugArray(String tag, String msg, int[] b, int lineLength) {
		if ((debug & ARRAY_MASK) != 0) {
			System.out.println("[" + tag + "] " + msg + "(" + b.length + ")");
			System.out.printf("%4x: ", 0);
			for (int i = 0; i < b.length; i++) {
				System.out.printf("%9x ", b[i]);
				if ((i + 1) % lineLength == 0) {
					System.out.println();
					System.out.printf("%4x: ", i+1);
				}
			}
			System.out.println();
		}
	}
	

	public static void debugArray(String tag, String msg, double[][] b, int width, int height) {
		if ((debug & ARRAY_MASK) != 0) {
			System.out.println("[" + tag + "] " + msg + "(" + width + " x " + height +")");
			for (int y = 0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					System.out.printf("%15f", b[x][y]);
				}
				System.out.println();
			}
			System.out.println();
		}
	}
	public static void debugArray(String tag, String msg, byte[][] b, int width, int height) {
		if ((debug & ARRAY_MASK) != 0) {
			System.out.println("[" + tag + "] " + msg + "(" + width + " x " + height +")");
			for (int y = 0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					System.out.printf("%4d", b[x][y]);
				}
				System.out.println();
			}
			System.out.println();
		}
	}
	public static void debugArray(String tag, String msg, int[][] b, int width, int height) {
		if ((debug & ARRAY_MASK) != 0) {
			System.out.println("[" + tag + "] " + msg + "(" + width + " x " + height +")");
			for (int y = 0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					System.out.printf("%9d", b[x][y]);
				}
				System.out.println();
			}
			System.out.println();
		}
	}
	public static void debugArray(String tag, String msg, byte[] b, int lineLength) {
		if ((debug & ARRAY_MASK) != 0) {
			System.out.println("[" + tag + "] " + msg + "(" + b.length + ")");
			System.out.printf("%4x: ", 0);
			for (int i = 0; i < b.length; i++) {
				System.out.printf("%4x ", b[i]);
				if ((i + 1) % lineLength == 0) {
					System.out.println();
					System.out.printf("%4x: ", i+1);
				}
			}
			System.out.println();
		}
	}
}
